package com.student.Activitys.activity;

import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.student.Activitys.R;
import com.student.Activitys.adapter.ActivityPagerAdapter;
import com.student.Activitys.utils.Constant;

import org.simple.eventbus.Subscriber;
import org.simple.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 主界面Activity
 * <p>
 * 将 4个 Activity 与 viewPager 结合， 进行 滑动、点击 监听
 */
public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener, ViewPager.OnPageChangeListener {

    @Bind(R.id.vp_main)
    ViewPager vpMain;
    @Bind(R.id.bottom_navigation_bar)
    BottomNavigationBar bottomNavigationBar;

    //管理者
    private LocalActivityManager manager = null;
    //跳转实例
    private Intent intentHome, intentCs, intentMailList, intentMy;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_main);
        ButterKnife.bind(this);
        //初始化数据
        initView(savedInstanceState);
        //adapter 加载 activity 具体 操作
        initAddActivitiesToViewPager();
    }


    //初始化数据
    private void initView(Bundle savedInstanceState) {
        //因为viewpager只能实现view或者fragment等的滑动，所以需要将activity利用此管理器转换成view
        manager = new LocalActivityManager(this, true);
        //此方法必须被调用，否则会报错
        manager.dispatchCreate(savedInstanceState);
        //设置 底部导航栏 样式
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        //为底部导航栏 装填数据
        bottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.ic_home, "首页").setActiveColorResource(R.color.colorAccent))
                .addItem(new BottomNavigationItem(R.mipmap.ic_conversation, "会话").setActiveColorResource(R.color.colorAccent))
                .addItem(new BottomNavigationItem(R.mipmap.ic_mail_list, "通讯录").setActiveColorResource(R.color.colorAccent))
                .addItem(new BottomNavigationItem(R.mipmap.ic_my, "我的").setActiveColorResource(R.color.colorAccent))
                .setFirstSelectedPosition(0)
                .initialise();
        bottomNavigationBar.setTabSelectedListener(this);
    }

    /**
     * 将 activity 以 view 的方式 放入 adapter
     */
    private void initAddActivitiesToViewPager() {
        //首页
        intentHome = new Intent(MainActivity.this, HomeActivity.class);
        View home = manager.startActivity("home", intentHome).getDecorView();
        //会话
        intentCs = new Intent(MainActivity.this, ConversationActivity.class);
        View conversation = manager.startActivity("conversation", intentCs).getDecorView();
        //通讯录
        intentMailList = new Intent(MainActivity.this, MailListActivity.class);
        View mailList = manager.startActivity("mailList", intentMailList).getDecorView();
        //我的
        intentMy = new Intent(MainActivity.this, MyActivity.class);
        View my = manager.startActivity("my", intentMy).getDecorView();

        //View 集合
        List<View> mViews = new ArrayList<View>();
        //将页面添加到View集合
        mViews.add(home);           //首页
        mViews.add(conversation);   //会话
        mViews.add(mailList);       //通讯录
        mViews.add(my);             //个人中心
        //装填 View 集合 数据
        ActivityPagerAdapter adapter = new ActivityPagerAdapter(mViews);
        // 配置适配器
        vpMain.setAdapter(adapter);
        //滑动事件监听
        vpMain.setOnPageChangeListener(this);
    }

    /**
     * BottomNavigationBar 点击事件
     *
     * @param position
     */
    @Override
    public void onTabSelected(int position) {
        //当BottomNavigationBar控件的某个标签被选中的时候，需要相应的选中ViewPager的对应页面。
        vpMain.setCurrentItem(position);
    }
    @Override
    public void onTabUnselected(int position) {
    }
    @Override
    public void onTabReselected(int position) {
    }

    /**
     * ViewPage 滑动事件
     *
     * @param position
     * @param positionOffset
     * @param positionOffsetPixels
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }
    @Override
    public void onPageSelected(int position) {
        //当ViewPager的某个页面被选中时，需要相应的选中BottomNavigationBar控件的对应标签。
        bottomNavigationBar.selectTab(position);
    }
    @Override
    public void onPageScrollStateChanged(int state) {
    }

    /**
     * 收到 退出 通知 退出主体页面
     */
    @Subscriber(tag = Constant.EXIT_MAIN, mode = ThreadMode.MAIN)
    public void initTestingOnCompleted(String str) {
       finish();
    }

}